package com.zjc.app.api.sdk.client;

import com.google.gson.reflect.TypeToken;
import com.zjc.app.api.sdk.dto.ResultDto;
import com.zjc.app.api.sdk.dto.StatisticInfoForFarm;
import com.zjc.app.api.sdk.dto.request.ApiCalOverloadPwdReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDeviceSecondResponsePushReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDevicesLockStateReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDevicesStateReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiDynamicQRCodeDTO;
import com.zjc.app.api.sdk.dto.request.ApiDynamicQRCodeReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiQueryEsDevicePositionDto;
import com.zjc.app.api.sdk.dto.request.ApiRegionReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiSearchTowerPropertiesReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiSecondResponseReqDTO;
import com.zjc.app.api.sdk.dto.request.ApiTowerResetPwdDTO;
import com.zjc.app.api.sdk.dto.request.ApiTowerResetPwdPropertiesReqDTO;
import com.zjc.app.api.sdk.dto.request.QueryDevicePositionsReqDTO;
import com.zjc.app.api.sdk.dto.result.ApiCommandResultDTO;
import com.zjc.app.api.sdk.dto.result.ApiDeviceInfoDTO;
import com.zjc.app.api.sdk.dto.result.ApiDeviceLockStateDTO;
import com.zjc.app.api.sdk.dto.result.ApiDevicePositionCmdResultDTO;
import com.zjc.app.api.sdk.dto.result.ApiDevicePositionDTO;
import com.zjc.app.api.sdk.dto.result.ApiDeviceStateDTO;
import com.zjc.app.api.sdk.dto.result.ApiDynamicQRCodeResultDTO;
import com.zjc.app.api.sdk.dto.result.ApiOverloadPwdResultDTO;
import com.zjc.app.api.sdk.dto.result.ApiSecondResponseDTO;
import com.zjc.app.api.sdk.dto.result.ApiTowerResetPwdResultDTO;
import com.zjc.app.api.sdk.dto.result.DeviceHistoryPositionDto;
import com.zjc.app.api.sdk.dto.result.DeviceRegionDetailDTO;
import com.zjc.app.api.sdk.dto.result.DeviceRegionStatisticsDto;
import com.zjc.app.api.sdk.dto.result.EsDevicePositionRes;
import com.zjc.app.api.sdk.dto.result.FarmDeviceStateRes;
import com.zjc.app.api.sdk.exception.AppBizRuntimeException;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DeviceClient extends BaseClient {

    public DeviceClient(String serverUrl, String appId, String appSecret, Integer connectTimeout, Integer readTimeout) {
        super(serverUrl, appId, appSecret, connectTimeout, readTimeout);
    }

    /**
     * 获取设备二次应答结果
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<ApiSecondResponseDTO>> getDeviceSecondResponse(ApiSecondResponseReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/second_response.list";
        return super.doPost(url, request, new TypeToken<ResultDto<List<ApiSecondResponseDTO>>>() {
        });
    }

    /**
     * 设备二次应答推送任务启动
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> startDeviceSecondResponsePush(ApiDeviceSecondResponsePushReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/second_response/push.start";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {
        });
    }

    /**
     * 设备二次应答推送任务停止
     *
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> stopDeviceSecondResponsePush() throws IOException, AppBizRuntimeException {
        String url = "/v1/device/second_response/push.stop";
        return super.doPost(url, null, new TypeToken<ResultDto<String>>() {
        });
    }

    /**
     * 查询终端状态和解锁机状态接口
     *
     * @param vehicleNo 设备编号
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiDeviceStateDTO> getDeviceState(String vehicleNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/state";
        Map<String, String> params = new TreeMap<>();
        params.put("vehicleNo", vehicleNo);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiDeviceStateDTO>>() {
        });
    }

    /**
     * 批量查询终端状态和解锁机状态接口
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<ApiDeviceStateDTO>> getDevicesState(ApiDevicesStateReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/state";
        return super.doPost(url, request, new TypeToken<ResultDto<List<ApiDeviceStateDTO>>>() {
        });
    }

    /**
     * 查询设备锁车状态(批量)
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<ApiDeviceLockStateDTO>> getDevicesLockState(ApiDevicesLockStateReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices/lock_state";
        return super.doPost(url, request, new TypeToken<ResultDto<List<ApiDeviceLockStateDTO>>>() {
        });
    }

    /**
     * 根据终端编号查询设备信息
     *
     * @param terminalNo 设备编号
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiDeviceInfoDTO> getDeviceInfoByTerminalNo(String terminalNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/info.by_terminal_no";
        Map<String, String> params = new TreeMap<>();
        params.put("terminalNo", terminalNo);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiDeviceInfoDTO>>() {
        });
    }

    /**
     * 根据终端编号查询设备信息
     *
     * @param terminalNo 设备编号
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiDevicePositionDTO> getDevicePositionByTerminalNo(String terminalNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/position.by_terminal_no";
        Map<String, String> params = new TreeMap<>();
        params.put("terminalNo", terminalNo);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiDevicePositionDTO>>() {
        });
    }

    /**
     * 发送定位指令获取设备位置信息接口
     *
     * @param vehicleNo 设备编号
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiDevicePositionCmdResultDTO> getDevicePositionCmdResult(String vehicleNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/cmd/position";
        Map<String, String> params = new TreeMap<>();
        params.put("vehicleNo", vehicleNo);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiDevicePositionCmdResultDTO>>() {
        });
    }

    /**
     * 申请超载解锁密码接口
     *
     * @param request
     * @return 密码
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<String> calDeviceOverloadPwd(ApiCalOverloadPwdReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/overload_pwd.cal";
        return super.doPost(url, request, new TypeToken<ResultDto<String>>() {
        });
    }

    /**
     * 超载密码计算参数查询接口
     *
     * @param vehicleNo 设备编号
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiOverloadPwdResultDTO> checkDeviceOverloadPwd(String vehicleNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/overload_pwd.check";
        Map<String, String> params = new TreeMap<>();
        params.put("vehicleNo", vehicleNo);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiOverloadPwdResultDTO>>() {
        });
    }

    /**
     * 塔机TSM二微码扫描动态码下发
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiDynamicQRCodeResultDTO> dynamicQRCodeSend(ApiDynamicQRCodeDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/DynamicQRCode.sendCmd";
        return super.doPost(url, request, new TypeToken<ResultDto<ApiDynamicQRCodeResultDTO>>() {
        });
    }

    /**
     * 塔机下发重置密码
     *
     * @param request 塔机下发重置密码请求
     * @return 塔机下发重置密码响应
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiTowerResetPwdResultDTO> towerResetPassword(ApiTowerResetPwdDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/tower/pwd.reset";
        return super.doPost(url, request, new TypeToken<ResultDto<ApiTowerResetPwdResultDTO>>() {
        });
    }

    /**
     * 塔机TSM二微码扫描查询下发结果
     *
     * @param msgId 指令结果ID
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<ApiCommandResultDTO> dynamicQRCodeSendResult(String msgId) throws IOException, AppBizRuntimeException {
        String url = "/v1/command/result";
        Map<String, String> params = new TreeMap<>();
        params.put("resultId", msgId);
        return super.doGet(url, params, new TypeToken<ResultDto<ApiCommandResultDTO>>() {
        });
    }

    /**
     * 塔机TSM二微码扫描动态码下发工况结果查询
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<Map<String, String>> dynamicQRCodeWorkResult(ApiDynamicQRCodeReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/DynamicQRCode.workResult";
        return super.doPost(url, request, new TypeToken<ResultDto<Map<String, String>>>() {
        });
    }

    /**
     * 塔机下发重置密码工况结果查询
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<Map<String, String>> towerResetPasswordResult(ApiTowerResetPwdPropertiesReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/tower/pwd/reset_result";
        return super.doPost(url, request, new TypeToken<ResultDto<Map<String, String>>>() {
        });
    }

    /**
     * 获取塔机一键寻塔指令下发的工况结果接口
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<Map<String, String>> searchTowerWorkingProperties(ApiSearchTowerPropertiesReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/search_tower/properties";
        return super.doPost(url, request, new TypeToken<ResultDto<Map<String, String>>>() {
        });
    }

    public ResultDto<Map<String, String>> getBatchLockStatus(List<String> request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/batchLockStatus";
        return super.doPost(url, request, new TypeToken<ResultDto<Map<String, String>>>() {
        });
    }

    public ResultDto<List<EsDevicePositionRes>> getDevicePosition(ApiQueryEsDevicePositionDto request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices.position";
        return super.doPost(url, request, new TypeToken<ResultDto<List<EsDevicePositionRes>>>() {
        });
    }

    /**
     * 获取设备历史轨迹
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<DeviceHistoryPositionDto>> queryHistoryPositions(QueryDevicePositionsReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/device/historyPositions";
        return super.doPost(url, request, new TypeToken<ResultDto<List<DeviceHistoryPositionDto>>>() {
        });
    }

    /**
     * 设备分布
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<List<DeviceRegionStatisticsDto>> queryDeviceRegionStatistics(ApiRegionReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices.regionStatistics";
        return super.doPost(url, request, new TypeToken<ResultDto<List<DeviceRegionStatisticsDto>>>() {
        });
    }

    /**
     * 设备分布 详情
     *
     * @param request
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<DeviceRegionDetailDTO> queryDeviceRegionDetail(ApiRegionReqDTO request) throws IOException, AppBizRuntimeException {
        String url = "/v1/devices.regionDetail";
        return super.doPost(url, request, new TypeToken<ResultDto<DeviceRegionDetailDTO>>() {
        });
    }

    /**
     * 设备 详情
     *
     * @param uniqueNo
     * @return
     * @throws IOException
     * @throws AppBizRuntimeException
     */
    public ResultDto<FarmDeviceStateRes> deviceStateDetail(String uniqueNo) throws IOException, AppBizRuntimeException {
        String url = "/v1/deviceState.detail";
        Map<String, String> params = new TreeMap<>();
        params.put("uniqueNo", uniqueNo);
        return super.doGet(url, params, new TypeToken<ResultDto<FarmDeviceStateRes>>() {
        });
    }

    /**
     * 给农场版提供统计接口
     * @return
     */
    public ResultDto<StatisticInfoForFarm> statisticIotData() throws IOException, AppBizRuntimeException {
        String url = "/v1/device/statisticInfo";
        return super.doGet(url, null, new TypeToken<ResultDto<StatisticInfoForFarm>>() {
        });
    }
}
